<template>
 
  <view class="goods-list">
    <view v-for="(item,i) in goodsList" :key="i"
    @click="gotoDetail(item)">
     <my-goods :goods="item"></my-goods>
    </view>
  </view>     
 
</template>

<script>
  export default {
    data() {
      return {
        queryObj:{
          // 查询关键字
          query:'',
          // 商品分类id
          cid:'',
              
          // 页数索引
           pagenum: 1,
           // 每页长度
           pagesize:10
        },
         // 商品列表的数据
            goodsList: [],
            // 总数量，用来实现分页
            total: 0,
             // 是否正在请求数据
                isloading: false
            // 默认图片地址
              // defaultPic: 'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png'
      };
    },
    onLoad(options) {
 
      // 调用获取商品列表数据的方法
       // 将页面参数转存到 this.queryObj 对象中
        this.queryObj.query = options.query || ''
        this.queryObj.cid = options.cid || ''
      this.getGoodsList()
      // console.log('onload执行了');
    },
     methods:{
      async getGoodsList(cb){
         this.isloading = true
       const {data:res}= await uni.$http.get('/api/public/v1/goods/search',this.queryObj)
        this.isloading = false
        cb&&cb()
       if(res.meta.status!==200) return uni.$showMsg()
       this.goodsList=[...this.goodsList,...res.message.goods]
       this.total=res.message.total
       // console.log(res);
       },
       gotoDetail(item){
         // console.log(item);
         uni.navigateTo({
          
          url: '/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
         })
       }
     },
     // 触底的事件
     onReachBottom(){
          if(this.queryObj.pagenum * this.queryObj.pagesize >=this.total) return uni.$showMsg('数据加载完毕!')
        if (this.isloading) return
         // 让页码值自增 +1
       this.queryObj.pagenum += 1
        this.getGoodsList()
     },
     onPullDownRefresh(){
       // 重置关键数据
         this.queryObj.pagenum = 1
         this.total = 0
         this.isloading = false
         this.goodsList = []
       // 重新发起请求
       this.getGoodsList(() => uni.stopPullDownRefresh())
     },
    
  }
</script>

<style lang="scss">

</style>
